<?php
/**
 * Created by PhpStorm.
 * User: Zhoujinda
 * Date: 2016/3/24
 * Time: 15:55
 */

namespace app\bbs\api;

use app\bbs\api\ForumUserAdmingroupApi;
use app\bbs\api\ForumAdmingroupApi;
use app\bbs\model\BbsMemberView;
use app\bbs\model\ForumAdmingroup;
use app\bbs\model\VUserInfo;
use app\common\api\BaseApi;
use app\system\api\MemberFocusApi;
use app\shop\api\MemberConfigApi;

class BbsMemberApi extends BaseApi{

    /**
     * 添加
     */
    const ADD = "bbs/BbsMemberApi/add";
    /**
     * 保存
     */
    const SAVE = "bbs/BbsMemberApi/save";
    /**
     * 保存根据ID主键
     */
    const SAVE_BY_ID = "bbs/BbsMemberApi/saveByID";

    /**
     * 删除
     */
    const DELETE = "bbs/BbsMemberApi/delete";

    /**
     * 查询
     */
    const QUERY = "bbs/BbsMemberApi/query";
    /**
     * 查询一条数据
     */
    const GET_INFO = "bbs/BbsMemberApi/getInfo";

    /**
     * 插入多条数据
     */
    const ADD_ALL = "bbs/BbsMemberApi/addAll";

    /**
     * 无分页查询
     */
    const QUERY_NO_PAGING = "bbs/BbsMemberApi/queryNoPaging";

    /**
     * 取用户信息
     */
    const GET_USER_INFO = "bbs/BbsMemberApi/getUserInfo";

    /**
     * 取用户信息分页
     */

    const QUERY_MEMBER = "bbs/BbsMemberApi/memberQuery";

    protected function _init(){
        $this -> model = new BbsMemberView();
    }


    /**
     * 取用户信息 - 带论坛权限信息 - 运行缓慢
     */
    public function getUserInfo($uid,$group=true,$focus=true,$fans=true,$vip=true){

        // $cacheKey = 'vuinfo-'.$uid;
        // $cache    = json_decode(S($cacheKey),true);
        // if(!is_null($cache)){
        //     $info =$cache;
        // }else{
            $model = new VUserInfo();
            $map = [
                'id' => $uid
            ];
            $field = "id,username,nickname,head,sign,realname,bg_img,score,sex";
            $result = $model->where($map)->field($field)->find();

            if(false === $result){
                return $this->apiReturnErr($model->getDbError());
            }
            if(empty($result)) return $this->apiReturnErr('用户不存在');
            $result['score'] = getNumberFormat($result['score']);
            // S($cacheKey,json_encode($result));
            $info = $result;
        // }

        if(empty($info['bg_img'])) $info['bg_img'] = '';
        //关注数
        if($focus){
            $result = apiCall(MemberFocusApi::COUNT,[['uid'=>$uid]]);
            if(!$result['status']) return $this->apiReturnErr($result['info']);
            $info['focus'] = getNumberFormat($result['info']);
        }
        //粉丝数
        if($fans){
            $result = apiCall(MemberFocusApi::COUNT,[['focus_uid'=>$uid]]);
            if(!$result['status']) return $this->apiReturnErr($result['info']);
            $info['be_focus'] = getNumberFormat($result['info']);
        }
        //用户等级 1=>vip1,2=>vip2 //0=>未知
        if($vip){
            $r = apiCall(MemberConfigApi::GET_VIP,[$uid]);
            if(!$r['status']) return $this->apiReturnErr($r['info']);
            $info['vip_info'] = $r['info'];
        }
        //查权限
        if($group){
            $map = ['uid'=>$uid];
            $info['group'] = [];
            $result = apiCall(ForumUserAdmingroupApi::GET_INFO,[$map]);
            if($result['status']){
                if(!empty($result['info'])){
                    //查询权限详情
                    $fid = $result['info']['fid'];
                    $map = ['adminid'=>$result['info']['adminid']];
                    $result = apiCall(ForumAdmingroupApi::GET_INFO,[$map]);
                    if($result['status']){
                        if(!empty($result['info'])){
                            $info['group'] = $result['info'];
                            $info['group']['fid'] = $fid;
                        }
                    }else{
                        return $this->apiReturnErr($result['info']);
                    }
                }else{
                    $info['group']['temp'] = '';
                }
            }
        }
        return $this->apiReturnSuc($info);
    }

}